#pragma once
#include "hashtable.hpp"
namespace xzq
{
    template <class K, class V, class Hash = OPEN_HASH::_Hash<K>>
    class unordered_map
    {
        struct MapOfT
        {
            const K &operator()(const pair<K, V> &kv)
            {
                return kv.first;
            }
        };

    public:
        typedef typename OPEN_HASH::HashTable<K, pair<K, V>, MapOfT, Hash>::iterator iterator;
        iterator begin()
        {
            return _ht.begin();
        }
        iterator end()
        {
            return _ht.end();
        }
        pair<iterator, bool> Insert(const pair<K, V> &kv)
        {
            return _ht.Insert(kv);
        }
        V &operator[](const K &key)
        {
            pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
            return ret.first->second;
        }

    private:
        OPEN_HASH::HashTable<K, pair<K, V>, MapOfT, Hash> _ht;
    };
    void testmap()
    {
        unordered_map<string, string> dict;
        dict.Insert(make_pair("sort", "排序"));
        dict.Insert(make_pair("left", "左边"));
        dict.Insert(make_pair("string", "字符串"));
        dict["left"] = "剩余";
        dict["end"] = "尾部";
        auto it = dict.begin();
        while (it != dict.end())
        {
            cout << it->first << " : " << it->second << endl;
            ++it;
        }
        cout << endl;
    }
}